﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using System.Xml.Serialization;
using System.Xml;

namespace XCBPM.Mobile.BLL
{
    public class MobileOfficeHelper
    {
        private static NetAttachmentType CheckUplaodUrl(string fileName)
        {
            string extension = Path.GetExtension(fileName);
            NetAttachmentType type = NetAttachmentType.Attach_OTHER;
            switch (extension.ToLower())
            {
                case ".doc":
                case ".docx":
                    return NetAttachmentType.Attach_WORD;

                case ".xls":
                case ".xlsm":
                case ".xlsx":
                    return NetAttachmentType.Attach_EXCEL;

                case ".ppt":
                case ".pptx":
                    return NetAttachmentType.Attach_PPT;
            }
            return type;
        }

        private static void CreateDictory(string dicPath)
        {
            string path = HttpContext.Current.Server.MapPath(dicPath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        public static string SerializeXml<T>(T entity)
        {
            System.Text.StringBuilder XmlData = new System.Text.StringBuilder();
            XmlSerializer xs = new XmlSerializer(entity.GetType());

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            XmlWriterSettings xst = new XmlWriterSettings();
            xst.OmitXmlDeclaration = true;

            using (XmlWriter writer = XmlWriter.Create(XmlData, xst))
            {
                xs.Serialize(writer, entity, ns);
                writer.Close();
            }

            return XmlData.ToString();
        }
        /// <summary>
        /// 插入XML一节点
        /// </summary>
        /// <param name="xmlPath">Xml文档路径</param>
        /// <param name="MainNode">当前节点路径</param>
        /// <param name="Element">新节点</param>
        /// <param name="Content">新节点值</param>
        public static void XmlInsertElement(XmlDocument objXmlDoc, string MainNode, string Element, string Content)
        {

            XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
        }

        public static void XmlInsertElement(XmlDocument objXmlDoc, string MainNode, string Element, string AttributeID, string AttributeText)
        {
            XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.SetAttribute(AttributeID, AttributeText);
            objNode.AppendChild(objElement);
        }

        /// <summary>
        /// 向一个节点添加属性
        /// </summary>
        /// <param name="xmlPath">xml文件路径</param>
        /// <param name="NodePath">节点路径</param>
        /// <param name="NodeAttribute1">要添加的节点属性的名称</param>
        /// <param name="NodeAttributeText">要添加属性的值</param>
        public static void AddAttribute(XmlDocument objXmlDoc, string NodePath, string NodeAttribute1, string NodeAttributeText)
        {
            XmlAttribute nodeAttribute = objXmlDoc.CreateAttribute(NodeAttribute1);
            XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
            nodePath.Attributes.Append(nodeAttribute);
            XmlElement xe = (XmlElement)nodePath;
            xe.SetAttribute(NodeAttribute1, NodeAttributeText);
        }

        private static void writeTestLog(string mess)
        {
            string dicPath = "~/Services/uploads/";
            CreateDictory(dicPath);
            FileStream stream = null;
            try
            {
                stream = new FileStream(HttpContext.Current.Server.MapPath(dicPath) + "log.txt", FileMode.Append, FileAccess.Write);
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine(mess);
                writer.Close();
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }

        public enum NetAttachmentType
        {
            Attach_OTHER,
            Attach_EXCEL,
            Attach_PPT,
            Attach_WORD
        }
    }
}